package com.example.mypplication.opengl

import android.opengl.GLES20
import android.opengl.GLSurfaceView
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class MyGLRenderer : GLSurfaceView.Renderer {
    private lateinit var triangle: Triangle
    /*
    // 顶点着色器代码
    private val vertexShaderCode = """
        attribute vec4 vPosition;
        void main() {
          gl_Position = vPosition;
        }
    """.trimIndent()

    // 片段着色器代码
    private val fragmentShaderCode = """
        precision mediump float;
        uniform vec4 vColor;
        void main() {
          gl_FragColor = vColor;
        }
    """.trimIndent()
*/
    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        // 设置背景色
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f)

        // 初始化三角形
        triangle = Triangle()
    }

    override fun onDrawFrame(gl: GL10?) {
        // 重绘背景色
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)

        // 绘制三角形
        triangle.draw()
    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        GLES20.glViewport(0, 0, width, height)
    }

    companion object {
        // 顶点着色器代码
         val vertexShaderCode = """
        attribute vec4 vPosition;
        void main() {
          gl_Position = vPosition;
        }
    """.trimIndent()

        // 片段着色器代码
         val fragmentShaderCode = """
        precision mediump float;
        uniform vec4 vColor;
        void main() {
          gl_FragColor = vColor;
        }
    """.trimIndent()

        fun loadShader(type: Int, shaderCode: String): Int {
            // 创建着色器
            val shader = GLES20.glCreateShader(type)

            // 添加着色器代码并编译
            GLES20.glShaderSource(shader, shaderCode)
            GLES20.glCompileShader(shader)

            return shader
        }
    }
}
/*

class MyGLRenderer : GLSurfaceView.Renderer {
    private lateinit var triangle: Triangle

    // 将着色器代码改为公开常量
    companion object {
        // 顶点着色器代码
        const val VERTEX_SHADER_CODE = """
            attribute vec4 vPosition;
            void main() {
              gl_Position = vPosition;
            }
        """.trimIndent()

        // 片段着色器代码
        const val FRAGMENT_SHADER_CODE = """
            precision mediump float;
            uniform vec4 vColor;
            void main() {
              gl_FragColor = vColor;
            }
        """.trimIndent()
    }

    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f)
        triangle = Triangle()
    }

    override fun onDrawFrame(gl: GL10?) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
        triangle.draw()
    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        GLES20.glViewport(0, 0, width, height)
    }

    fun loadShader(type: Int, shaderCode: String): Int {
        val shader = GLES20.glCreateShader(type)
        GLES20.glShaderSource(shader, shaderCode)
        GLES20.glCompileShader(shader)
        return shader
    }
}*/
